package com.weavernorth.salestasks;

import com.weavernorth.sysap.util.SyncUtil;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.workflow.webservices.*;

/**
 * @Author: BruseeWang
 * @Date: 2021/4/25 14:54
 * @Description: 【品牌】品牌单元&分公司月度销售任务制定流程 自动创建
 */
public class SalesTaskCreateFlowTimmer  extends BaseCronJob {
    @Override
    public void execute() {
        Logger log =Logger4jUtil.getLoggerByName("/Timmer/SalesTaskCreateFlowTimmer/");
        log.info("=======定时任务：自动创建流程-【品牌】品牌单元&分公司月度销售任务制定流程=======");
        BaseBean bb=new BaseBean();
        //默认创建人
        String strCreator=bb.getPropValue("salsProp", "creator");
        String strNowDate=dateUtils.getYearMonthDay();
        log.info("当前年月："+strNowDate);
        String nowTime= dateUtils.getHourMs();
        log.info("当期时间："+nowTime);
        String strDate=dateUtils.previousMonth();
        log.info("上月日期年月："+strDate);
        String strYear = "";
        String strMonth="";
        if (!"".equals(strDate)) {
            strYear = strDate.substring(0,4);
            strMonth=strDate.substring(4,6);
        }
        String strQuerySql="SELECT * FROM uf_pp_ppdyrwcfjl where nf='"+strYear+"' and yf='"+Integer.parseInt(strMonth)+"'";
        log.info("查询是否存在sql："+strQuerySql);
        RecordSet rs_query=new RecordSet();
        rs_query.execute(strQuerySql);
        Integer number=0;
        while (rs_query.next()){
            number++;
        }
        if (number>0){
            log.info(strYear+"年"+strMonth+"月的月度销售任务制定流程已创建,请勿重复创建！");
        }
        else {
            try {
                SyncUtil syncUtil = new SyncUtil();
                String strFormid = syncUtil.getModeIdBytableName("uf_pp_ppdyrwcfjl");
                String strSql = "SELECT\n" +
                        "\t main.NF,\n" +
                        "\t main.yf,\n" +
                        "\t py.ppdydl,\n" +
                        "\t py.ppdy,\n" +
                        "\t py.fgfz,\n" +
                        "\t py.zl,\n" +
                        "\t py.px,\n" +
                        "\t SUM(dt1.XSRWE) dyxdrw\n" +
                        "\t \n" +
                        "FROM\n" +
                        "\tUF_PP_PPDY py\n" +
                        "\tINNER JOIN UF_RWXDB_YD main ON py.ppdy = main.FGS \n" +
                        "\tAND py.sfqy = 1\n" +
                        "\tINNER JOIN UF_RWXDB_YD_DT1 dt1 ON main.id = dt1.mainid \n" +
                        "WHERE\n" +
                        "\tYF = '" + strMonth + "' \n" +
                        "\tAND NF = '" + strYear + "'\n" +
                        "GROUP BY main.NF,\n" +
                        "\t main.yf,\n" +
                        "\t py.ppdydl,\n" +
                        "\t py.ppdy,\n" +
                        "\t py.fgfz,\n" +
                        "\t py.zl,\n" +
                        "\t py.px order by py.px asc ";
                log.info("数据源sql:" + strSql);
                RecordSet rs = new RecordSet();
                rs.execute(strSql);
                //查询建模表应该触发的所有品牌单元的行数
                String str_getAll = "SELECT COUNT(id) counts FROM UF_PP_PPDY WHERE sfqy=1";
                RecordSet rs_AllCount = new RecordSet();
                rs_AllCount.executeQuery(str_getAll);
                String strCounts="0";
                while (rs_AllCount.next()){
                    strCounts=Util.null2String(rs_AllCount.getString("counts"));
                }
                log.info("结果集："+strCounts);
                Integer all_count = Integer.valueOf(strCounts);
                log.info("应该触发的所有品牌单元为：" + all_count);
                log.info("实际触发的品牌单元为：" + rs.getCounts());
                //查询品牌单元&分公司月度销售任务制定流程 触发记录表
                if (rs.getCounts() == all_count) {
                    //region [封装主表]
                    WorkflowRequestTableField[] wrMain = new WorkflowRequestTableField[3];
                    //提交日期
                    wrMain[0] = new WorkflowRequestTableField();
                    wrMain[0].setFieldName("tjrq");
                    wrMain[0].setView(true);
                    wrMain[0].setEdit(true);
                    wrMain[0].setFieldOrder(0);
                    wrMain[0].setMand(false);
                    wrMain[0].setFieldValue(strNowDate);
                    //年份
                    wrMain[1] = new WorkflowRequestTableField();
                    wrMain[1].setFieldName("nf");
                    wrMain[1].setView(true);
                    wrMain[1].setEdit(true);
                    wrMain[1].setFieldOrder(0);
                    wrMain[1].setMand(false);
                    wrMain[1].setFieldValue(strYear);
                    //月份
                    wrMain[2] = new WorkflowRequestTableField();
                    wrMain[2].setFieldName("yf");
                    wrMain[2].setView(true);
                    wrMain[2].setEdit(true);
                    wrMain[2].setFieldOrder(0);
                    wrMain[2].setMand(false);
                    wrMain[2].setFieldValue(strMonth);
                    //endregion

                    String workflowid = bb.getPropValue("salsProp", "workflowid");
                    log.info("workflowid:" + workflowid);

                    //配置流程基本信息:workflowId
                    WorkflowBaseInfo wbi = new WorkflowBaseInfo();
                    wbi.setWorkflowId(workflowid);
                    wbi.setWorkflowName("【品牌】品牌单元&分公司月度销售任务制定流程");//流程名称

                    //填装主表相关字段对象
                    WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
                    wrtri[0] = new WorkflowRequestTableRecord();
                    wrtri[0].setWorkflowRequestTableFields(wrMain);

                    //创建流程主表信息对象
                    WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
                    wmi.setRequestRecords(wrtri);
                    WorkflowRequestInfo wrInfo = new WorkflowRequestInfo();
                    wrInfo.setCreatorId(strCreator);//流程创建者
                    wrInfo.setRequestLevel("0");//请求重要级别 0：正常 1：重要 2：紧急
                    wrInfo.setRequestName("【品牌】品牌单元&分公司月度销售任务制定流程");//请求的名称
                    //0:停留在提交节点;1:自动流转到下一节点
                    wrInfo.setIsnextflow("1");
                    //主表数据
                    wrInfo.setWorkflowMainTableInfo(wmi);
                    wrInfo.setWorkflowBaseInfo(wbi);//工作流信息


                    //定义明细表个数
                    WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[1];
                    log.info("明细表行数：" + rs.getCounts());
                    wrtri = new WorkflowRequestTableRecord[rs.getCounts()];
                    WorkflowRequestTableField[] wrti1;
                    Integer row = 0;
                    while (rs.next()) {
                        //明细表1
                        wrti1 = new WorkflowRequestTableField[3];

                        //分公司或单元
                        wrti1[0] = new WorkflowRequestTableField();
                        wrti1[0].setFieldName("fgshppdanyu");
                        wrti1[0].setFieldValue(Util.null2String(rs.getString("ppdy")));
                        wrti1[0].setView(true);
                        wrti1[0].setEdit(true);
                        log.info("分公司或单元：" + Util.null2String(rs.getString("ppdy")));

                        //分管副总
                        wrti1[1] = new WorkflowRequestTableField();
                        wrti1[1].setFieldName("ppzj");
                        wrti1[1].setFieldValue(Util.null2String(rs.getString("fgfz")));
                        wrti1[1].setView(true);
                        wrti1[1].setEdit(true);
                        log.info("分管副总：" + Util.null2String(rs.getString("fgfz")));

                        //助理
                        wrti1[2] = new WorkflowRequestTableField();
                        wrti1[2].setFieldName("zl");
                        wrti1[2].setFieldValue(Util.null2String(rs.getString("zl")));
                        wrti1[2].setView(true);
                        wrti1[2].setEdit(true);
                        log.info("助理：" + Util.null2String(rs.getString("zl")));

                        //当月下达任务(元)
                        wrti1[3] = new WorkflowRequestTableField();
                        wrti1[3].setFieldName("dyxdrwy");
                        wrti1[3].setFieldValue(Util.null2String(rs.getString("dyxdrw")));
                        wrti1[3].setView(true);
                        wrti1[3].setEdit(true);
                        log.info("当月下达任务：" + Util.null2String(rs.getString("dyxdrw")));

                        wrtri[row] = new WorkflowRequestTableRecord();
                        wrtri[row].setWorkflowRequestTableFields(wrti1);
                        row++;
                    }
                    wdti[0] = new WorkflowDetailTableInfo();
                    wdti[0].setWorkflowRequestTableRecords(wrtri);

                    wrInfo.setWorkflowDetailTableInfos(wdti);
                    wrInfo.setCreatorId(strCreator);
                    WorkflowServiceImpl wfsi = new WorkflowServiceImpl();
                    String requestid = wfsi.doCreateWorkflowRequest(wrInfo, Integer.parseInt(strCreator));
                    log.info("【品牌】品牌单元&分公司月度销售任务制定流程的requestid==" + requestid);
                    if (!"".equals(requestid) && Integer.parseInt(requestid) > 0) {
                        String strInsert_sql="insert into uf_pp_ppdyrwcfjl (nf,yf,requestid,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) values('"+strYear+"','"+Integer.parseInt(strMonth)+"','"+requestid+"','"+strFormid+"',1,0,'"+strNowDate+"','"+nowTime+"')";
                        log.info("插入建模表数据sql："+strInsert_sql);
                        RecordSet rs_insert=new RecordSet();
                        rs_insert.execute(strInsert_sql);
                        log.info("流程创建成功！");
                    } else {
                        log.info("流程创建失败！");
                    }
                } else {
                    log.info("实际触发的品牌单元与应该触发的不匹配,不创建流程！");
                }

            } catch (Exception ex) {
                log.info("创建流程出现异常,异常信息：" + ex.toString());
            }
        }
        log.info("定时任务---【品牌】品牌单元&分公司月度销售任务制定流程 创建结束--。。。");
    }
}
